using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponControllerRemote : WeaponControllerBase
{
    [Header("----- 后坐力设置 -----")]
    [SerializeField] protected float recoilDuration;
    [SerializeField] protected float recoilDistance;
    
    protected override void WeaponPeformAttack(Vector2 attackDirection)
    {
        base.WeaponPeformAttack(attackDirection);

        weaponAttackCo ??= StartCoroutine(PerformRecoilCo(attackDirection));
    }
    
    private IEnumerator PerformRecoilCo(Vector2 attackDirection)
    {
        canAiming = false;
        //武器移动的距离
        Vector2 posOffset = -attackDirection * recoilDistance;
        //武器目标移动位置（局部坐标）
        Vector2 targetPos = new(transform.localPosition.x + posOffset.x, transform.localPosition.y + posOffset.y);

        Vector2 orgiginalPos = transform.localPosition;

        float timer = 0;

        while (timer < recoilDuration)
        {
            transform.localPosition = Vector2.Lerp(orgiginalPos, targetPos, timer / recoilDuration);
            timer += Time.deltaTime;
            yield return null;
        }
        transform.localPosition = targetPos;

        timer = 0;

        while (timer < recoilDuration)
        {
            transform.localPosition = Vector2.Lerp(targetPos, orgiginalPos, timer / recoilDuration);
            timer += Time.deltaTime;
            yield return null;
        }
        //防止偏差
        transform.localPosition = orgiginalPos;
        canAiming = true;

        weaponAttackCo = null;
    }
}
